package com.qf.controller;


import com.qf.entity.Hotal;
import com.qf.entity.OrderDetailResult;
import com.qf.entity.OrderParams;
import com.qf.entity.R;
import com.qf.service.HotalService;
import com.qf.util.BloomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * (Hotal)表控制层
 *
 * @author makejava
 * @since 2020-11-13 09:12:12
 */
@RestController
@RequestMapping("/hotal")
public class HotalController {
    /**
     * 服务对象
     */
    @Autowired
    private HotalService hotalService;

    @Autowired
    private BloomUtil bloomUtil;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @RequestMapping("/list")
    public R<List<Hotal>> queryList(){
        //获得酒店列表
        List<Hotal> hotals = hotalService.list();
        return new R<List<Hotal>>().setData(hotals);
    }

    @RequestMapping("/insert")
    public R<Boolean> insert(@RequestBody Hotal hotal){
        hotalService.save(hotal);
        return new R<Boolean>().setData(true);
    }

    /**
     * 1w个用户
     * 10W个酒店
     * @param hid
     * @param devid
     * @return
     */
    @RequestMapping("/getHotal")
    public R<Hotal> getHotal(Integer hid, String devid){
        Hotal hotal = hotalService.getById(hid);

        //TODO 计算hid对应酒店的点击率 - 布隆过滤器
        String val = devid + "_" + hid;

        //判断是否点击过
//        if (!bloomUtil.isExists("hotal_clicks_bloom", val)) {
//            System.out.println(devid  + "没有点击过该酒店：" + hid);
//
//            bloomUtil.addBloom("hotal_clicks_bloom", val);

            //判断酒店的点击率是否记录过
            if(!redisTemplate.opsForHash().hasKey("hotal_clicks", hid + "")){
                //第一次记录该酒店的点击率
                redisTemplate.opsForHash().put("hotal_clicks", hid + "", "1");
            } else {
                //对酒店的点击率进行自增
                redisTemplate.opsForHash().increment("hotal_clicks", hid + "", 1);
            }

//        } else {
//            System.out.println(devid  + "已经点击过该酒店：" + hid);
//        }

        return new R<Hotal>().setData(hotal);
    }

    /**
     * 获得预订酒店房间的价格明细
     * @return
     */
    @RequestMapping("/getRoomPrice")
    public R getRoomPrice(OrderParams orderParams){
        OrderDetailResult result = hotalService.orderPrice(orderParams);
        return new R().setData(result);
    }

    @RequestMapping("/getRoomPriceFeign")
    public R<OrderDetailResult> getRoomPriceFeign(@RequestBody OrderParams orderParams){
        OrderDetailResult result = hotalService.orderPrice(orderParams);
        return new R().setData(result);
    }
}